package com.cuitrip.component.item;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorRes;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.app.MainApplication;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.utils.DisplayUtil;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * 标准item 布局
 * Created by ak
 */
public class ItemLayout extends FrameLayout {

    private Context mContext;

    public static final int FULL = 0;
    public static final int NONE = 1;
    public static final int CONTENT = 2;

    public static final int TEXT = 0;

    public static final int ICON = 1;

    @Bind(R.id.content_layout)
    View mContentLayout;

    @Bind(R.id.left_icon)
    IconTextView mLeftIcon;

    @Bind(R.id.left_title)
    TextView mLeftTitle;

    @Bind(R.id.right_text)
    TextView mRightText;

    @Bind(R.id.right_icon)
    TextView mRightIcon;


    View bottomDivideLine;

    View rightLine;


    private boolean isShowLeftIcon;

    private
    @StringRes
    int leftIconRes;

    private
    int leftIconColor;

    private
    @StringRes
    int leftTitleRes;

    private
    int rightTextColor;

    /*private
    @ColorRes
    int rightResColor;
*/

    private String rightText;

    private boolean showRightIcon;

    private boolean showRightLine;

    private int bottomLineStyle;
    private int rightTextStyle;

    private
    @StringRes
    int rightTextRes;
    //private @StringRes int rightTextDesc;

    public ItemLayout(Context context) {
        this(context, null);
    }

    public ItemLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInternal(attrs, context, defStyleAttr);
    }

    private void initInternal(AttributeSet attrs, Context context, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemLayout, defStyle, R.style.ItemLayout_Default);


        isShowLeftIcon = a.getBoolean(R.styleable.ItemLayout_show_left_icon, false);
        leftIconColor = a.getColor(R.styleable.ItemLayout_left_icon_color, ResourceUtil.getColor(R.color.qiaomai_7c706e));
        leftIconRes = a.getResourceId(R.styleable.ItemLayout_left_icon, -1);
        leftTitleRes = a.getResourceId(R.styleable.ItemLayout_left_title_res, -1);

        rightTextColor = a.getColor(R.styleable.ItemLayout_right_text_color, ResourceUtil.getColor(R.color.qiaomai_7c706e));

//        rightResColor=a.getResourceId(R.styleable.ItemLayout_right_res_color,-1);
        rightTextRes = a.getResourceId(R.styleable.ItemLayout_right_text_res, -1);

        showRightIcon = a.getBoolean(R.styleable.ItemLayout_show_right_hint, false);

        showRightLine = a.getBoolean(R.styleable.ItemLayout_show_right_line, true);

        bottomLineStyle = a.getInt(R.styleable.ItemLayout_bottom_line_style, -1);


        rightTextStyle = a.getInt(R.styleable.ItemLayout_right_text_style, -1);

        a.recycle();

        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.item_layout, this);
        ButterKnife.bind(this);

        bindDataView();

    }

    public void invalidateView() {
        bindDataView();
    }

    private void bindDataView() {
        if (isShowLeftIcon) {
            mLeftIcon.setVisibility(VISIBLE);
            mLeftIcon.setTextColor(leftIconColor);

            if (leftIconRes != -1) {
                mLeftIcon.setText(getResources().getString(leftIconRes));
            }
        } else {
            mLeftIcon.setVisibility(GONE);
        }

        if (leftTitleRes != -1) {
            mLeftTitle.setText(getResources().getString(leftTitleRes));
        }


        mRightText.setTextColor(rightTextColor);

        if (rightTextRes != -1) {
            mRightText.setText(getResources().getString(rightTextRes));
        }
        if (showRightIcon) {
            mRightIcon.setVisibility(VISIBLE);
        } else {
            mRightIcon.setVisibility(GONE);
        }
        bottomDivideLine = new View(mContext);
        bottomDivideLine.setBackgroundColor(getResources().getColor(R.color.ganshi_ded8d7));
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
        layoutParams.gravity = Gravity.BOTTOM;

        if (bottomLineStyle == CONTENT) {
            layoutParams.setMargins(DisplayUtil.dip2px(16.f), 0, DisplayUtil.dip2px(16.f), 0);
        }
        if (bottomLineStyle != NONE) {
            bottomDivideLine.setLayoutParams(layoutParams);
            addView(bottomDivideLine);
        }

        if (rightTextStyle == ICON) {
            mRightText.setTypeface(MainApplication.getInstance().getIconFont());
        }

        rightLine = new View(mContext);
        rightLine.setBackgroundColor(getResources().getColor(R.color.ganshi_ded8d7));
        FrameLayout.LayoutParams rightLineLayoutParams = new FrameLayout.LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT);
        rightLineLayoutParams.gravity = Gravity.RIGHT;

        if (showRightLine) {
            rightLine.setLayoutParams(rightLineLayoutParams);
            addView(rightLine);
        }

    }

    public void setShowRightIcon(boolean showRightIcon) {
        this.showRightIcon = showRightIcon;
        mRightIcon.setVisibility(showRightIcon ? VISIBLE : GONE);
    }

    public void setmRightText(CharSequence titleText) {
        mRightText.setText(titleText);
    }

    public void setleftText(CharSequence text) {
        mLeftTitle.setText(text);
    }

    public void setBottomLineStyle(int style) {
        if (style == NONE) {
            bottomDivideLine.setVisibility(GONE);
        } else if (style == CONTENT) {
            FrameLayout.LayoutParams layoutParams = (LayoutParams) bottomDivideLine.getLayoutParams();
            layoutParams.setMargins(DisplayUtil.dip2px(16.f), 0, DisplayUtil.dip2px(16.f), 0);
            bottomDivideLine.setLayoutParams(layoutParams);
        } else if (style == FULL) {
            FrameLayout.LayoutParams layoutParams = (LayoutParams) bottomDivideLine.getLayoutParams();
            layoutParams.setMargins(0, 0, 0, 0);
            bottomDivideLine.setLayoutParams(layoutParams);
        }
    }

    public void setRightTextColor(@ColorRes int colorRes) {
        mRightText.setTextColor(getResources().getColor(colorRes));
    }

    public void setRightColor(int color) {
        mRightText.setTextColor(color);
    }

    public TextView getmRightTextView() {
        return mRightText;
    }

    public CharSequence getmRightText() {
        return mRightText.getText();
    }

    public void setRightTextSize(float size) {
        mRightText.setTextSize(size);
    }
}
